<?php
defined('_XR_EXEC') or die();
/**
* 日志类
*
*  by zhoupeng 2011-7-18
*/
class logs{
	static function note($data='',$title='日志',$newdir='') {
		$_dir = XR_APP_LOG;
		if($newdir != ''){
			$_dir .= $newdir.DS;
		}
		$_file = $_dir.date('Ymd').'.log';
		# 目录不存在，建立目录
		if (!is_dir($_dir)){
			if (!self::mkdirs($_dir) || !is_writable($_dir)) {
				trigger_error('Directory dose not writable.', 'E_USER_WARNING');
				return false;
			}
		}
		if(is_null($data)){
			$data = '[is_null]';
		}else{
			$data = var_export($data,true);
		}
		$data = date('Y-m-d H:i:s').' '.$title.' '.$data."\r\n";
		$handle = @fopen($_file, 'at+');
		if (@flock($handle, LOCK_EX)) { //LOCK_EX 独占锁定
			fwrite($handle, $data);//sleep(6);
			flock($handle, LOCK_UN);
			fclose($handle); 
		} else {
			//trigger_error('File:' . basename($_file) . ' is busing now....<br>', 'E_USER_WARNING');
			return false;
		}
		return true;
	} 
	static function mkdirs($dir, $mode = 0777) {
		if (is_dir($dir) || @ mkdir($dir, $mode))
			return true;
		else {
			if (self::mkdirs(dirname($dir), $mode) === true)
				return self::mkdirs($dir);
		}
	}	
}	 
?>